package com.dao.impl;

import com.dao.UserDao;
import com.db.ConnectionFactory;
import com.entry.User;

import javax.naming.NamingException;
import java.sql.*;

public class UserDaoImpl implements UserDao {
    @Override
    public int add(String username) {
        int i=0;
        Connection conn = null;
        String sql = "insert into user(username,userpass,usergroup) values(?,?,?)";
        PreparedStatement prst = null;
        try {
            conn = ConnectionFactory.getConnection();
            prst = conn.prepareStatement(sql);
            prst.setString(1, username);
            prst.setString(2, "000000");
            prst.setString(3, "员工");
            i = prst.executeUpdate();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                if (prst != null)
                    prst.close();
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return i;
    }

    @Override
    public User find(String username, String userpass) {
        User user = null;
        Connection conn = null;
        PreparedStatement prst = null;
        ResultSet rs = null;
        String sql = "select * from user where username=? and userpass=? limit 1";
        try {
            conn = ConnectionFactory.getConnection();
            prst = conn.prepareStatement(sql);
            prst.setString(1, username);
            prst.setString(2, userpass);
            rs = prst.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setUserid(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setUserpass(rs.getString(3));
                user.setUsergroup(rs.getString(4));
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (prst != null)
                    prst.close();
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    @Override
    public int update(String pass, String username) {
        int row = 0;
        Connection con = null;
        PreparedStatement pre = null;
        String sql = "update user set userpass=? where username=?";
        try {
            con = ConnectionFactory.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, pass);
            pre.setString(2, username);
            row = pre.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pre != null)
                    pre.close();
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return row;
    }
}
